package com.zj.mapping;

import com.zj.utils.Configuration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DynamicSqlSource implements SqlSource{
    private String sql;
    private Configuration configuration;

    public DynamicSqlSource(Configuration configuration, String sql) {
        this.configuration = configuration;
        this.sql = sql;
    }
    public BoundSql getBoundSql(Object parameterObject) {
        String sql1=sqlReplace(sql,parameterObject);
        SqlSource sqlSource =sqlParse(sql1,parameterObject);
        BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
        return boundSql;
    }

    public String sqlReplace(String originalSql, Object parameterObject) {
        TokenParser tokenParser = new TokenParser();
        return tokenParser.parseSqlBinding(originalSql,parameterObject);
    }

    public SqlSource sqlParse(String originalSql,Object parameterObject) {
        TokenParser tokenParser = new TokenParser();
        String sqlPrepared=tokenParser.parseSqlAndGetParamMapppingList(originalSql,parameterObject);
        return new StaticSqlSource(configuration, sqlPrepared,tokenParser.getParamMappingList());
    }
}
